import fetch from 'dva/fetch';

// const host = 'http://127.0.0.1:7001'
const host = 'http://sign.atony2099.com'

async function checkStatus(response) {

  if (response.status >= 200 && response.status < 300) {
    response = await response.json();
    return response;
  }

  // todo : 这里有严重的问题=======
  console.log(response,"response======");
  response = await response.json();
  const error = new Error(response.error_msg || '网络错误');

  throw error;
}

/**
 * Requests a URL, returning a promise.
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 * @return {object}           An object containing either "data" or "err"
 */
async function request(url, options) {
  options.headers = {
    Accept: 'application/json',
    'Content-Type': 'application/json; charset=utf-8',
  }

  try {
    let token =  localStorage.getItem('TOKEN');
    options.headers.token = token;

  } catch (error) {
    
  }
 
  const response = await fetch(host + url, options);

  const data = await checkStatus(response)
  

  return data.data;


}

export default request;
